﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Data;

namespace Enough.Converter
{
    public class TranslationConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (parameter == null)
            {
                return value;
            }
            string key = parameter.ToString();
            var resourceLoader = new Windows.ApplicationModel.Resources.ResourceLoader();
            // first try to load specific translation by using databound value.
            // example: I have two resource: numberOfAlbums = "{0} albums" and numberOfAlbums1 = "One album"
            string translation = resourceLoader.GetString(key + value);
            if (string.IsNullOrEmpty(translation))
            {
                translation = resourceLoader.GetString(key);
            }
            if (string.IsNullOrEmpty(translation))
            {
                translation = key;
            }
            else
            {
                translation = string.Format(translation, value);
            }
            return translation;

        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
